<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use App\Middleware\JWTAuthMiddleware;
use App\Services\ChatService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([JWTAuthMiddleware::class])]
#[Controller(prefix: '/api/v1/chat')]
class ChatController extends CController
{
    #[Inject]
    protected ChatService $chatService;

    /**
     * 聊天列表.
     */
    #[RequestMapping(path: 'list', methods: 'get')]
    public function list()
    {
        $data = $this->chatService->list($this->request->all());
        return $this->success($data, $this->request);
    }

    /**
     * 聊天详情.
     */
    #[RequestMapping(path: 'info', methods: 'get')]
    public function info()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
        ]);
        $data = $this->chatService->info($params);
        return $this->success($data, $this->request);
    }

    /**
     * 置顶聊天.
     */
    #[RequestMapping(path: 'top', methods: 'put')]
    public function top()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
            'is_top' => 'required',
        ]);
        $data = $this->chatService->top($params);
        return $this->success($data, $this->request);
    }

    /**
     * 隐藏聊天.
     */
    #[RequestMapping(path: 'hide', methods: 'put')]
    public function hide()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
        ]);
        $data = $this->chatService->hide($params);
        return $this->success($data, $this->request);
    }

    /**
     * 设置聊天.
     */
    #[RequestMapping(path: 'update', methods: 'put')]
    public function update()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
        ]);
        $this->chatService->update($params);
        return $this->success([], $this->request);
    }

    /**
     * 删除聊天.
     */
    #[RequestMapping(path: 'delete', methods: 'delete')]
    public function delete()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'to_user' => 'required',
            'is_group' => 'required',
        ]);
        $data = $this->chatService->delete($params);
        return $this->success($data, $this->request);
    }
}
